package lukeer.fencing;

import csproj.fencing.Move;

import java.util.ArrayList;

/**
 * Class generates moves for a given player (BLACK or WHITE) for a board object.
 *
 * User: Luke Richardson lukeer, Wafa M'Barek wmbarek
 * Date: 07/10/12
 * Time: 14:39
 */
class MoveGenerator {
    public static ArrayList<Move> generateMoves(Board b, int player) {
        ArrayList<Pair> pieces = b.getEmptyPositions();

        if (pieces.size() == 0) return generateFence(b, player);
        return generatePieceAndFence(b, player);
    }

    /**
     * Generate a list of moves based on the available piece placements and fence placements
     *
     * @param b      board to generate moves for
     * @param player player move to be created for
     * @return list of move objects
     */
    private static ArrayList<Move> generatePieceAndFence(Board b, int player) {
        ArrayList<Pair> pieces = b.getEmptyPositions();
        ArrayList<Pair> horiz = b.getEmptyHFences();
        ArrayList<Pair> vert = b.getEmptyVFences();
        ArrayList<Move> moves = new ArrayList<Move>();

        for (Pair piece : pieces) {
            moves.add((new Move(piece.getX(), piece.getY(), player, true, -1, -1, false, 0)));
            for (Pair hFence : horiz) {
                moves.add((new Move(piece.getX(), piece.getY(), player, true, hFence.getX(), hFence.getY(), true, 2)));
            }
            for (Pair vFence : vert) {
                moves.add((new Move(piece.getX(), piece.getY(), player, true, vFence.getX(), vFence.getY(), true, 1)));
            }
        }
        return moves;
    }

    /**
     * Generate a list of moves based on the available fence placements
     *
     * @param b      board to generate fences for
     * @param player player move to be created for
     * @return list of move objects
     */
    private static ArrayList<Move> generateFence(Board b, int player) {
        ArrayList<Pair> horiz = b.getEmptyHFences();
        ArrayList<Pair> vert = b.getEmptyVFences();
        ArrayList<Move> moves = new ArrayList<Move>();

        for (Pair hFence : horiz) {
            moves.add((new Move(-1, -1, player, false, hFence.getX(), hFence.getY(), true, 2)));
        }
        for (Pair vFence : vert) {
            moves.add((new Move(-1, -1, player, false, vFence.getX(), vFence.getY(), true, 1)));
        }
        return moves;
    }
}
